package com.ihg.hiex.entity.session.before.finaltest;

import java.beans.Transient;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Type;

import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.during.SessionExerciseAnchor;
import com.ihg.hiex.entity.user.Assessor;

/**
 * 答题结果
 *
 * @author Ark
 *
 */
@Entity
@Table(name = "final_test_score")
public class FinalTestScore {

    @Id
    @GeneratedValue
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Session session;
    
    /** 对应Assessor */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Assessor assessor;
    
    /** 对应大题 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private FinalTestExercise exercise;
    
    /** 打分描述 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private SessionExerciseAnchor anchor;
    
    /** 标准答案 */
    private String standAnswer;
    
    /** 标准答案理由说明 */
    @Type(type = "text")
    private String reason;
    
    /** 用户答案 */
    private String answer;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public FinalTestScore() {
    }

    /**
     * 
     * @param session
     * @param assessor
     * @param exercise
     * @param template
     */
    public FinalTestScore(Session session, Assessor assessor, FinalTestExercise exercise, FinalTestScoreTemplate template) {
        this.session = session;
        this.assessor = assessor;
        this.exercise = exercise;
        this.anchor = template.getAnchor();
        this.standAnswer = template.getAnswer();
        this.reason = template.getReason();
        this.disabled = false;
    }
    
    /**
     * 
     * @param assessor
     * @param exercise
     * @param template
     */
    public FinalTestScore(Assessor assessor, FinalTestExercise exercise, FinalTestScoreTemplate template) {
        this.assessor = assessor;
        this.exercise = exercise;
        this.anchor = template.getAnchor();
        this.standAnswer = template.getAnswer();
        this.reason = template.getReason();
        this.disabled = false;
    }
    
    /**
     * @return
     */
    public boolean hasAnswer() {
        return StringUtils.isNotBlank(answer);
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isAnswerRight() {
        return StringUtils.isNotBlank(answer) && StringUtils.equals(answer, standAnswer);
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isAnswerWrong() {
        return StringUtils.isNotBlank(answer) && !StringUtils.equals(answer, standAnswer);
    }
    
    /**
     * @param answer
     * @return
     */
    public FinalTestScore answer(String answer) {
        this.answer = answer;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @return the assessor
     */
    public Assessor getAssessor() {
        return assessor;
    }

    /**
     * @return the exercise
     */
    public FinalTestExercise getExercise() {
        return exercise;
    }

    /**
     * @return the anchor
     */
    public SessionExerciseAnchor getAnchor() {
        return anchor;
    }

    /**
     * @return the standAnswer
     */
    public String getStandAnswer() {
        return standAnswer;
    }

    /**
     * @return the reason
     */
    public String getReason() {
        return reason;
    }

    /**
     * @return the answer
     */
    public String getAnswer() {
        return answer;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @param assessor the assessor to set
     */
    public void setAssessor(Assessor assessor) {
        this.assessor = assessor;
    }

    /**
     * @param exercise the exercise to set
     */
    public void setExercise(FinalTestExercise exercise) {
        this.exercise = exercise;
    }

    /**
     * @param anchor the anchor to set
     */
    public void setAnchor(SessionExerciseAnchor anchor) {
        this.anchor = anchor;
    }

    /**
     * @param standAnswer the standAnswer to set
     */
    public void setStandAnswer(String standAnswer) {
        this.standAnswer = standAnswer;
    }

    /**
     * @param reason the reason to set
     */
    public void setReason(String reason) {
        this.reason = reason;
    }

    /**
     * @param answer the answer to set
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
}